package com.tentative.competition.generator;


import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class Generator{
    public static void main(String[] args) {
        String url = "jdbc:mysql://175.178.91.76:3306/tentative-competition?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
        String userName = "root";
        String pwd = "Liyuan@success";
        String outputDir = System.getProperty("user.dir") + "/tentative-competition-system/src/main/java";
        //生成实体类service mapper的位置
        String parentPackage = "com.tentative.competition";
        //对应的表名，可多个
        String[] tableNames = {"schedule_works"};
        //前缀过滤
        String tablePrefix = "";

        AutoGenerator mpg = new AutoGenerator();
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(outputDir);
        gc.setAuthor("SmileH");
        gc.setOpen(false);
        gc.setServiceName("%sService");
        mpg.setGlobalConfig(gc);

        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername(userName);
        dsc.setPassword(pwd);
        dsc.setTypeConvert(new MySqlTypeConvert(){
            @Override
            public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                if(fieldType.toLowerCase().contains("datetime") || fieldType.toLowerCase().contains("timestamp")){
                    return DbColumnType.DATE;
                }
                return super.processTypeConvert(globalConfig, fieldType);
            }
        });
        mpg.setDataSource(dsc);

        PackageConfig pc = new PackageConfig();
        pc.setParent(parentPackage);
        mpg.setPackageInfo(pc);

        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {

            }
        };
        mpg.setCfg(cfg);

        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setController(null);
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        //基类
        strategyConfig.setSuperEntityClass("com.tentative.competition.system.domain.Base");
        //过滤生成字段
        strategyConfig.setSuperEntityColumns("id","create_time","update_time","create_by","update_by");
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setInclude(tableNames);
        strategyConfig.setTablePrefix(tablePrefix);
        mpg.setStrategy(strategyConfig);
        mpg.execute();
    }
}
